﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices;
using System.IO;
using System.Xml;
using System.DirectoryServices.ActiveDirectory;
using System.Threading;
using System.Net;
using System.Text;
using System.Collections.ObjectModel;

namespace Main
{
    public class ControlProducts
    {
        public class Model
        {
            public class Product
            {
                private String _name;
                private String _type;
                private String _price;
                private int _id;

                public Product()
                {

                }
                public int Id
                {
                    get { return _id; }
                    set { _id = value; }
                }
                public String Name
                {
                    get { return _name; }
                    set { _name = value; }
                }
                public String Type
                {
                    get { return _type; }
                    set { _type = value; }
                }
                public String Price
                {
                    get { return _price; }
                    set { _price = value; }
                }
            }
        }

        public class Control
        {
            ControlCommon common = new ControlCommon();

            /// <summary>
            /// saves new product info on SQL Azure
            /// </summary>
            /// <param name="name"></param>
            /// <param name="type"></param>
            /// <param name="price"></param>
            /// <returns></returns>
            public String CreateNewProduct(string name, string type, int price, string companyLogin)
            {
                try
                {
                    common.CreateLog("Starting saving of New Product on SQL Azure for: " + name);
                    TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                    Product product = new Product();
                    product.name = name;
                    product.type = type;
                    product.price = price;
                    product.Company = companyLogin;
                    db.Products.InsertOnSubmit(product);
                    db.SubmitChanges();
                    db.Dispose();

                    common.CreateLog("Stopping Successful saving of New Product on SQL Azure for: " + name);
                    return String.Empty;
                }
                catch (Exception e)
                {
                    common.CreateLog(e.ToString());
                    return e.ToString();
                }
            }

            /// <summary>
            ///  gets product info from SQL Azure if any
            /// </summary>
            /// <returns></returns>
            public List<Product> ViewProduct(string companyLogin)
            {
                TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
                var query = from u in db.Products
                            orderby u.type, u.name ascending
                            where u.name != String.Empty && u.Company == companyLogin
                            select u;

                return query.ToList();
            }

            /// <summary>
            /// gets product info from SQL Azure if any as Dictionary
            /// </summary>
            /// <returns></returns>
            public Dictionary<int, String> ViewProductAsDict(string companyLogin)
            {
                Dictionary<int, String> list = new Dictionary<int, string>();

                TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();

                var custQuery = from p in db.Products
                                where p.Company == companyLogin
                                orderby p.name
                                select new
                                {
                                    p.ID,
                                    p.name,
                                };

                //Convert each record into ModelProduct object and store into list
                foreach (var record in custQuery)
                {
                    list.Add(record.ID, record.name);
                }

                return list;
            }
        }
    }
}